package org.chnxi.study.leetcode.questions50.ques33;

public class Simple01 {

    public static void main(String[] args) {
        int[] nums1 = {4,5,6,7,0,1,2};
        System.out.println(search(nums1, 1));
//
//        System.out.println(search(nums1, 0));

//        System.out.println(search(new int[]{1,0}, 0));
    }

    public static int search(int[] nums, int target) {
        int startIdx = 0;
        int endIdx = nums.length-1;
        if(nums.length < 1 || (target<nums[startIdx] && target > nums[endIdx])){
            return -1;
        }
        return s(nums , 0 , nums.length-1 , target);
    }

    private static int s(int[] nums , int start ,int end , int target){

        if(start > end ){
            return -1;
        }

        int mid = (end + start) / 2;

        if(target == nums[start]){
            return start;
        }else if(target == nums[mid]){
            return mid;
        }else if(target == nums[end]){
            return end;
        }
        if(mid == start){
            return -1;
        }
        if(target > nums[start]){
            return s(nums , start , mid , target);
        }else if(start < nums[end]){
            return s(nums , mid , end , target);
        }
        return -1;
    }

}
